#pragma once
#include<string>
#include<iostream>
#include<vector>
#include<ctemplate/template.h>
#include"model.hpp"

using namespace ns_model;
const std::string ctemplate_path="./ctemplate_html/";
namespace ns_view
{
    class View
    {
        public:
        View()
        {}
        ~View()
        {}
        public:
        void AllExpendHtml(const std::vector<Questions>& questions,std::string* html)
        {
            //1、形成源文件路径
            std::string src_html=ctemplate_path+"question_all.html";

            //2、形成数据字典
            ctemplate::TemplateDictionary root("question_all");
            for(const auto&q:questions)
            {
                ctemplate::TemplateDictionary*sub=root.AddSectionDictionary("question_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }
            //3、获取被渲染的对象
            ctemplate::Template*tpl=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //4、完成渲染
            tpl->Expand(html,&root);

        }
        void OneExpendHtml(const Questions& q,std::string*html)
        {
            //1、形成路经
            std::string src_html=ctemplate_path+"question_one.html";

            //2、形成数据字典
            ctemplate::TemplateDictionary root("question_one");
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);

            //3、获取被渲染的对象
            ctemplate::Template*tpl=ctemplate::Template::GetTemplate(src_html,ctemplate::DO_NOT_STRIP);

            //4、完成渲染
            tpl->Expand(html,&root);
            
        }
    };
}